package com.diapoclic.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.diapoclic.entitybean.User;
import com.diapoclic.entitymanager.FolderManager;
import com.diapoclic.entitymanager.UserManager;

public class PartageServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)						
											throws IOException, ServletException{

		
		UserManager userMana = new UserManager();
		User sessionUser = new User();
		FolderManager folderMana = new FolderManager();
		
		////////////////////////////////UTILISATION DES COOKIES///////////////////////////
		
		sessionUser=userMana.getSessionUser(request.getCookies());

		
		////////////////////////////////AFFICHAGE ALBUMS///////////////////////////
		
		try {
			request.setAttribute("albumlist",folderMana.getFolderDao().findByIdUser(sessionUser.getIdUser()));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		////////////////////////////////AFFICHAGE AMIS///////////////////////////
		
		try {
			request.setAttribute("friendlist",userMana.getUserDao().listFriend(sessionUser.getIdUser()));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		////////////////////////////////TRAITEMENT CASE A COCHER///////////////////////////
		String[] cbox = request.getParameterValues("name");
		String[] cboxalbum = request.getParameterValues("album");
		
		if(cbox != null && cboxalbum != null)
		{
			for (int i = 0; i < cbox.length; ++i)
			{
				for(int j = 0; j < cboxalbum.length; j++)
				{
					try {
						userMana.getUserDao().addFolderShared(Integer.parseInt(cboxalbum[j]), sessionUser.getIdUser(), Integer.parseInt(cbox[i]));
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		
		request.getRequestDispatcher("partage.jsp").forward(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)						
											throws IOException, ServletException{
		doGet(request, response);
	}	
	
}